<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class RegisterController extends Controller
{
    //前台注册
	public function getRegister()
	{
		return view('Home.register.register');
	}
	//注册成功
	public function getRegisterok()
	{
		return view('Home.register.registerok');
	}
	//执行注册
	public function postInsert(Request $request)
	{

		//检测手机验证码是否正确
		$code = $request->input('code');
		$scode = session('code');
		//dd($scode);
		if($code!=$scode){
			return back()->with('error','验证码错误') ;
		}
		//dd($request->all());
			//处理数据添加到数据库
        $data = $request->only(['username','password']);
        $data['token'] = str_random(50);
        //密码加密
        $data['password'] = Hash::make($data['password']);
        $dataa = $request->only(['username','phone']);
        //dd($dataa);
         //执行数据的插入
        $res = DB::table('user')->insert($data);
        $ress = DB::table('userdetail')->insert($dataa);
        
        if($res){
            return redirect('/home/register/registerok')->with('success','用户添加成功');
        }else{
            // echo '失败';
            return back()->with('error','用户添加失败');
        }
	}
	public function getAjax(Request $request)
	{
		
	//接收参数 
	$name = $request->only(['username']);

	//$pdo = new PDO('mysql:host=localhost;dbname=lamp157;charset=utf8','root','1225');


	//$stmt = $pdo->query('select * from user where username = "'.$name.'"');

	//提取结果
	//$res = $stmt->fetch();
	$res = DB::table('user')->where('username',$name)->first();
	//dd($res);
		if(empty($res)){
			//为空 可用
			echo 1;
		}else{
			echo 0;
		}
	}
	//手机ajax
	public function getPajax(Request $request)
	{
		
	//接收参数 
	$phone = $request->only(['phone']);
	
	//$pdo = new PDO('mysql:host=localhost;dbname=lamp157;charset=utf8','root','1225');


	//$stmt = $pdo->query('select * from user where username = "'.$name.'"');

	//提取结果
	//$res = $stmt->fetch();
	$res = DB::table('userdetail')->where('phone',$phone)->first();
	//dd($res);
		if(empty($res)){
			//为空 可用
			echo 1;
		}else{
			echo 0;
		}
	}

	
		
		//接受ajax请求 调用方法 发送验证码
	public function sendmess(Request $request)
	{
		//dd($request->all());
		//提取手机号
		$phone = $request->input('phone');
		//dd($phone);
		//随机验证码
		$code = rand(100000,999999);
		//将验证码存入session
		session(['code'=>$code]);

		$resss=$this->sendmessage($code,$phone);
		if($resss){
			echo 1;
		}else{
			echo 0;
		}
	}

	//发送短信验证
	public function sendmessage($code,$to)
	{

		//载入ucpass类
		//require_once('lib/Ucpaas.class.php');

		//初始化必填
		$options['accountsid']='1ca3762e86e59fe53e338cbabe88eabc';
		$options['token']='1be95e16f8e863c282fd30c778a61b75';


		//初始化 $options必填
		$ucpass = new \Ucpaas($options);

		//开发者账号信息查询默认为json或xml

		//echo $ucpass->getDevinfo('xml');



		//语音验证码,云之讯融合通讯开放平台收到请求后，向对象电话终端发起呼叫，接通电话后将播放指定语音验证码序列
		//$appId = "0e3a2e87c6404debb85f60444ecc5aa1";
		//$code = rand(100000,999999); 
		//$to = "18500406673";

		//echo $ucpass->voiceCode($appId,$code,$to);

		//短信验证码（模板短信）,默认以65个汉字（同65个英文）为一条（可容纳字数受您应用名称占用字符影响），超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
		$appId = "0e3a2e87c6404debb85f60444ecc5aa1";//引用id
		//$to = "18500406673";//发送给谁
		$templateId = "31795";//短信模板的id
		//$code = rand(100000,999999); 
		$param="中药材天地网,".$code.",3";//三个参数123

		$res= $ucpass->templateSMS($appId,$to,$templateId,$param);
		//$res = '{"resp":{"respCode":"000000","templateSMS":{"createDate":"20161109162455","smsId":"cb3d8b421593c90d75d57f38a9a3c9ea"}}}';
		$arr = json_decode($res,true);
		if($arr['resp']['respCode']=='000000'){
			return true;
		}else{
			return false;
		}
	}
	///忘记密码
	 public function getForgetpass()
	 {
	 	return view('Home.register.forgetpass');
	 }
	 //获取验证码
	 public function postDoforgetpass(Request $request)
	 {
	 	
		//dd($request->all());
		//获取电话号
		$phone = $request->input('phone');
		//查询
		$res = DB::table('userdetail')->where('phone',$phone)->first();
		$id = $res->id;
		//dd($id);
		//获取session中的验证码
		$code=session('code');
		//获取提交的验证码
		$vcode = $request->input('code');
		//判断
		if($code==$vcode){
			// return redirect('/home/register/findpass');
			// dd($id);
			return view('Home.register.findpass',['id'=>$id]);
		}else{
			 return back()->with('error','验证码错误');
		}
	 }
	 //找回密码
	 

	 public function postDofindpass(Request $request)
	 {
	 	//$pass = $request->input('password');
	 	$id = $request->only(['id']);
	 	$data = Hash::make($request->input('password'));

	 	$res = DB::table('user')->where('id',$id)->update(['password',$data]);
	 	if($res){
        return redirect('/home/')->with('success','修改成功');
    	}else{
    		return back()->with('error','修改失败');
    	}
	 	
	 }
}

	